package com.machine.filling.function.init.login;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.machine.filling.Test.TestCommon;
import com.machine.filling.db.dao.ClientInfoDao;
import com.machine.filling.net.http.network.APIHttpManager;
import com.machine.filling.net.http.network.BaseHttpStringCallback;
import com.machine.filling.net.http.response.BaseHttpResponseBean;
import com.machine.filling.net.http.response.LoginResBean;
import com.machine.filling.util.CustomToast;

import okhttp3.Call;

/**
 * Created by LENOVO on 2017/12/16.
 */

public class LoginPresenter implements LoginContract.Presenter {
    private Context mContext;
    private LoginContract.View mView;

    public LoginPresenter(Context mContext, LoginContract.View mView) {
        this.mContext = mContext;
        this.mView = mView;
        mView.setPresenter(this);
    }

    @Override
    public void login() {
        String loginName = mView.getLoginName();
        String loginPsw = mView.getLoginPsw();
        String devicNum;
        if (TestCommon.isTest) {
            devicNum = TestCommon.realDeviceNum;
        }else {
            devicNum = mView.getDeviceNum();
        }
        if (!verifayLoginInfo(loginName, loginPsw, devicNum)) {
            return;
        }
        mView.showProgress(null);
        APIHttpManager.getInstance().login(loginName, loginPsw, devicNum, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                LoginResBean resBean = new Gson().fromJson(result, LoginResBean.class);
                if (resBean.getResult() != null) {
                    ClientInfoDao.getInstance().updataDeviceId(resBean.getResult().getOilClient().getId());
                    if ("1".equals(resBean.getResult().getIsBind())){
                        //已绑定 ： 进入添加油枪界面
                        mView.showBindedResult();
                    }else {
                        //未绑定 ：跳转查询油站信息界面
                        mView.showUnBindedResult();
                    }
                }else {
                    //极端情况
                    CustomToast.showToast(mContext,resBean.getMsg());
                }
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                CustomToast.showToast(mContext,errorMsg);
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }

    @Override
    public void fillerLogin() {
        String loginName = mView.getLoginName();
        String loginPsw = mView.getLoginPsw();
        String sellId = ClientInfoDao.getInstance().getModleByDeviceNum().getStationId();
        if (!verifayLoginInfo(loginName, loginPsw, "补位")) {
            return;
        }
        mView.showProgress(null);
        APIHttpManager.getInstance().fillerLogin(loginName, loginPsw, sellId, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                mView.showFillerLoginSuccess();
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                CustomToast.showToast(mContext,errorMsg);
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }

    /**
     * 检测 输入信息 和设备id
     *
     * @param loginName
     * @param loginPsw
     * @param deviceNum
     */
    private boolean verifayLoginInfo(String loginName, String loginPsw, String deviceNum) {
        if (TextUtils.isEmpty(deviceNum)) {
            CustomToast.showToast(mContext, "设备id获取失败");
            return false;
        } else if (TextUtils.isEmpty(loginName)) {
            CustomToast.showToast(mContext, "请输入登录名");
            return false;
        } else if (TextUtils.isEmpty(loginPsw)) {
            CustomToast.showToast(mContext, "请输入登录密码");
            return false;
        } else {
            return true;
        }

    }

    @Override
    public void start() {

    }

    @Override
    public void distoryContext() {


    }


}
